package com.ruoyi.carbon.service.impl;

import java.util.List;

import com.ruoyi.carbon.domain.ScmPurchaseApply;
import com.ruoyi.carbon.domain.ScmPurchaseSupplier;
import com.ruoyi.carbon.mapper.*;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.carbon.domain.ScmPurchaseContractDetail;
import com.ruoyi.carbon.domain.ScmPurchaseContract;
import com.ruoyi.carbon.service.IScmPurchaseContractService;

/**
 * 采购合同Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-22
 */
@Service
public class ScmPurchaseContractServiceImpl implements IScmPurchaseContractService 
{
    @Autowired
    private ScmPurchaseContractMapper scmPurchaseContractMapper;
    @Autowired
    private ScmPurchaseSupplierMapper scmPurchaseSupplierMapper;
    @Autowired
    private WmsMaterialInfoMapper wmsMaterialInfoMapper;
    @Autowired
    private ScmPurchaseApplyMapper scmPurchaseApplyMapper;

    /**
     * 查询采购合同
     * 
     * @param id 采购合同主键
     * @return 采购合同
     */
    @Override
    public ScmPurchaseContract selectScmPurchaseContractById(Long id)
    {
        ScmPurchaseContract scmPurchaseContract = scmPurchaseContractMapper.selectScmPurchaseContractById(id);
        List<ScmPurchaseContractDetail> list = scmPurchaseContract.getScmPurchaseContractDetailList();
        if (!(list.isEmpty())){
            list.forEach(detail -> {
                detail.setMaterialInfo(wmsMaterialInfoMapper.selectWmsMaterialInfoById(detail.getMaterialId()));
            });
        }
        return scmPurchaseContract;
    }

    /**
     * 查询采购合同列表
     * 
     * @param scmPurchaseContract 采购合同
     * @return 采购合同
     */
    @Override
    public List<ScmPurchaseContract> selectScmPurchaseContractList(ScmPurchaseContract scmPurchaseContract)
    {
        List<ScmPurchaseContract> list = scmPurchaseContractMapper.selectScmPurchaseContractList(scmPurchaseContract);
        list.forEach(i -> {
            ScmPurchaseSupplier scmPurchaseSupplier = scmPurchaseSupplierMapper.selectScmPurchaseSupplierById(i.getSupplierId());
            if (scmPurchaseSupplier != null){
                i.setSupplierName(scmPurchaseSupplier.getName());
            }

            ScmPurchaseApply scmPurchaseApply = scmPurchaseApplyMapper.selectScmPurchaseApplyById(i.getApplyId());
            if (scmPurchaseApply != null){
                i.setContract(scmPurchaseContract.getApplyUser());
            }

        });
        return list;
    }

    /**
     * 新增采购合同
     * 
     * @param scmPurchaseContract 采购合同
     * @return 结果
     */
    @Transactional
    @Override
    public int insertScmPurchaseContract(ScmPurchaseContract scmPurchaseContract)
    {
        scmPurchaseContract.setCreateTime(DateUtils.getNowDate());
        int rows = scmPurchaseContractMapper.insertScmPurchaseContract(scmPurchaseContract);
        insertScmPurchaseContractDetail(scmPurchaseContract);
        return rows;
    }

    /**
     * 修改采购合同
     * 
     * @param scmPurchaseContract 采购合同
     * @return 结果
     */
    @Transactional
    @Override
    public int updateScmPurchaseContract(ScmPurchaseContract scmPurchaseContract)
    {
//        scmPurchaseContractMapper.deleteScmPurchaseContractDetailByContractId(scmPurchaseContract.getId());
//        insertScmPurchaseContractDetail(scmPurchaseContract);
        return scmPurchaseContractMapper.updateScmPurchaseContract(scmPurchaseContract);
    }

    /**
     * 批量删除采购合同
     * 
     * @param ids 需要删除的采购合同主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScmPurchaseContractByIds(Long[] ids)
    {
        scmPurchaseContractMapper.deleteScmPurchaseContractDetailByContractIds(ids);
        return scmPurchaseContractMapper.deleteScmPurchaseContractByIds(ids);
    }

    /**
     * 删除采购合同信息
     * 
     * @param id 采购合同主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScmPurchaseContractById(Long id)
    {
        scmPurchaseContractMapper.deleteScmPurchaseContractDetailByContractId(id);
        return scmPurchaseContractMapper.deleteScmPurchaseContractById(id);
    }

    /**
     * 新增合同物料明细信息
     * 
     * @param scmPurchaseContract 采购合同对象
     */
    public void insertScmPurchaseContractDetail(ScmPurchaseContract scmPurchaseContract)
    {
        List<ScmPurchaseContractDetail> scmPurchaseContractDetailList = scmPurchaseContract.getScmPurchaseContractDetailList();
        Long id = scmPurchaseContract.getId();
        if (StringUtils.isNotNull(scmPurchaseContractDetailList))
        {
            List<ScmPurchaseContractDetail> list = new ArrayList<ScmPurchaseContractDetail>();
            for (ScmPurchaseContractDetail scmPurchaseContractDetail : scmPurchaseContractDetailList)
            {
                scmPurchaseContractDetail.setContractId(id);
                list.add(scmPurchaseContractDetail);
            }
            if (list.size() > 0)
            {
                scmPurchaseContractMapper.batchScmPurchaseContractDetail(list);
            }
        }
    }
}
